from datetime import datetime

# 获取当前本地日期和时间
now = datetime.now()
print(now)  # 2025-10-29 17:35:47.145555

print("=" * 50)
# 把datetime对象格式化为字符串
now_str = now.strftime("%Y-%m-%d %H:%M:%S")
print(now_str)  # 2025-10-29 17:35:47

print("=" * 50)
# 把字符串解析为datetime对象
date_str = "2025-10-01 10:12:10"
date_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print(date_obj) # 2025-10-01 10:12:10

print("=" * 50)
# 时间日期的运算，其中datetime.timedelta表示时间间隔，可以进行日期和时间的加减操作
from datetime import timedelta

future_date = datetime.now() + timedelta(days=10)
past_date = datetime.now() - timedelta(days=10)
print("10天后的日期:", future_date)
print("10天前的日期:", past_date)

print("=" * 50)
# 获取当前日期
today = datetime.now().today()
print(today)

# datetime中的一些属性
now = datetime.now()
print("当前年:", now.year)
print("当前月:", now.month)
print("当前日:", now.day)
print("当前小时:", now.hour)
print("当前分钟:", now.minute)
print("当前秒:", now.second)